package chapter01;

public class RemoveDuplicates {
    //暴力
    public int removeDuplicates(int[] nums) {
        int length=nums.length;
        int i=0;
        while (i<length){
            if(i+1<length&&nums[i]==nums[i+1]){
                int j=i+1;
                while (i+1<length&&nums[i]==nums[i+1]){
                    i++;
                }
                for(int k=++i;k<length;k++){
                    nums[k-(i-j)]=nums[k];
                }
                length-=(i-j);
                i=j;
            }else{
                i++;
            }
        }
        return length;
    }

    //双指针
    public int removeDuplicates1(int[] nums) {
        int i=0,j=0,length=nums.length;
        while (j<length){
            while (j+1<length&&nums[j]==nums[j+1]){
                j++;
            }
            nums[i++]=nums[j++];
        }
        return i;
    }

}
